<template>
  <div class="quick-access">
    <div class="section-header">
      <h3 class="section-title">
        <el-icon><Guide /></el-icon>
        快速访问
      </h3>
      <p class="section-description">常用功能快速入口</p>
    </div>
    <div class="access-grid">
      <div v-for="item in accessibleItems" :key="item.id" class="access-item" @click="handleItemClick(item)">
        <div class="item-icon" :style="{ backgroundColor: item.color + '20', color: item.color }">
          <el-icon :size="28">
            <component :is="item.icon" />
          </el-icon>
          <div v-if="item.isAiGenerated" class="ai-badge">AI</div>
        </div>
        <div class="item-content">
          <h4 class="item-title">{{ item.title }}</h4>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from "vue";
import { useRouter } from "vue-router";
import { useAuthStore } from "@/stores/modules/auth";
import { storeToRefs } from "pinia";
import { Guide } from "@element-plus/icons-vue";
import type { QuickAccessItem } from "../types/home";

interface Props {
  quickAccessItems: QuickAccessItem[];
}

const props = defineProps<Props>();
const router = useRouter();
const authStore = useAuthStore();
const { authMenuListGet } = storeToRefs(authStore);

// 递归查找菜单树中是否存在指定路径的菜单
const findMenuInTree = (menuList: any[], targetPath: string): boolean => {
  for (const menu of menuList) {
    // 检查当前菜单的路径是否匹配
    if (menu.path === targetPath) {
      return true;
    }
    // 如果有子菜单，递归查找
    if (menu.children && menu.children.length > 0) {
      if (findMenuInTree(menu.children, targetPath)) {
        return true;
      }
    }
  }
  return false;
};

// 过滤有权限的访问项
const accessibleItems = computed(() => {
  return props.quickAccessItems.filter(item => {
    // 如果没有配置权限，默认显示
    if (!item.route) return true;

    // 在菜单树中查找是否存在该菜单
    return findMenuInTree(authMenuListGet.value, item.route);
  });
});

// 处理项目点击
const handleItemClick = (item: QuickAccessItem) => {
  if (item.route) {
    router.push(item.route);
  }
};
</script>

<style lang="scss" scoped>
.quick-access {
  display: flex;
  flex-direction: column;
  min-height: 400px;
  padding: 24px;
  background-color: var(--el-bg-color);
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 12px;
  box-shadow: 0 2px 12px rgb(0 0 0 / 10%);
  .section-header {
    margin-bottom: 20px;
    .section-title {
      display: flex;
      gap: 8px;
      align-items: center;
      margin: 0 0 8px;
      font-size: 18px;
      font-weight: 600;
      color: var(--el-text-color-primary);
      .el-icon {
        color: var(--el-color-primary);
      }
    }
    .section-description {
      margin: 0;
      font-size: 14px;
      color: var(--el-text-color-regular);
    }
  }
  .access-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    overflow: hidden auto;
  }
  .access-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 68px;
    padding: 12px 8px;
    text-align: center;
    cursor: pointer;
    background: var(--el-bg-color-page);
    border: 1px solid var(--el-border-color-lighter);
    border-radius: 10px;
    transition: all 0.3s ease;
    &:hover {
      background: var(--el-color-primary-light-9);
      border-color: var(--el-color-primary-light-7);
      box-shadow: 0 2px 8px rgb(0 0 0 / 12%);
      transform: translateY(-1px);
    }
    .item-icon {
      position: relative;
      display: flex;
      flex-shrink: 0;
      align-items: center;
      justify-content: center;
      width: 48px;
      height: 48px;
      margin-bottom: 8px;
      border-radius: 10px;
      .ai-badge {
        position: absolute;
        top: -4px;
        right: -4px;
        z-index: 10;
        padding: 1px 4px;
        font-size: 8px;
        font-weight: bold;
        color: white;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 6px;
        box-shadow: 0 1px 3px rgb(0 0 0 / 30%);
        transform: scale(0.8);
        animation: ai-pulse 2s infinite;
      }
    }
    .item-content {
      flex: 1;
      min-width: 0;
      .item-title {
        display: -webkit-box;
        margin: 0;
        overflow: hidden;
        font-size: 12px;
        font-weight: 500;
        line-height: 1.3;
        color: var(--el-text-color-primary);
        word-break: break-word;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
    }
  }
}

@keyframes ai-pulse {
  0% {
    opacity: 1;
    transform: scale(0.9);
  }
  50% {
    opacity: 0.8;
    transform: scale(1);
  }
  100% {
    opacity: 1;
    transform: scale(0.9);
  }
}

// 响应式设计
@media (width <= 768px) {
  .quick-access {
    padding: 20px;
    .access-grid {
      grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
      gap: 10px;
    }
    .access-item {
      padding: 10px 6px;
      .item-icon {
        width: 40px;
        height: 40px;
        margin-bottom: 6px;
        .el-icon {
          font-size: 20px;
        }
      }
      .item-content {
        .item-title {
          font-size: 11px;
        }
      }
    }
  }
}
</style>
